//------------------------------------------------------------------------------
// <pcdd-entitiy>
//     彩票主流业务类库
//     会员银行业务对象
// </pcdd-entitiy>
//------------------------------------------------------------------------------ 

using CL.Enum.Common;
using CL.Enum.Common.Type;
using CL.Game.DAL;
using CL.Game.Entity;
using CL.Json.Entity;
using CL.Json.Entity.WebAPI;
using CL.Redis.BLL;
using CL.Tools.Common;
using CL.View.Entity.Redis;
using System;
using System.Collections.Generic;
using System.Linq;

namespace CL.Game.BLL
{

    /// <summary>
    /// UsersBanksBLL 业务
    /// </summary>
    public class UsersBanksBLL
    {
        Log log = new Log("UsersBanksBLL");
        UsersBanksDAL dal = new UsersBanksDAL(DbConnectionEnum.CaileGame);
        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public long InertEntity(UsersBanksEntity entity)
        {
            return dal.InertEntity(entity);
        }

        /// <summary>
        /// 删除对象
        /// </summary>
        /// <param name="BankID"></param>
        /// <returns></returns>
        public bool DelEntity(long BankID)
        {
            return dal.DelEntity(BankID);
        }
        /// <summary>
        /// 查询对象
        /// </summary>
        /// <param name="UserCode"></param>
        /// <param name="CardNum"></param>
        /// <returns></returns>
        public UsersBanksEntity QueryEntityByBankNum(long UserCode, string CardNum)
        {
            return dal.QueryEntityByBankNum(UserCode, CardNum);
        }

        #region 自定义方法
        /// <summary>
        /// 绑定银行卡
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="BankName"></param>
        /// <param name="CardNum"></param>
        /// <param name="Area"></param>
        /// <param name="Mobile"></param>
        /// <param name="VerifyCode"></param>
        /// <returns></returns>
        public JsonResult BindBankCard(long UserCode, string BankName, string CardNum, string Area, string Mobile, string VerifyCode)
        {
            JsonResult result = null;
            try
            {
                int RecCode = (int)ResultCode.Success;
                if (this.QueryEntityByBankNum(UserCode, CardNum) != null)
                    RecCode = (int)ResultCode.IsBankCardExist;
                else
                {

                    int VerifyResult = new SystemRedis().VerifyCodeRedis(Mobile, VerifyCode, (int)VerifyTypeEnum.BandBank);
                    if (VerifyResult == (int)ResultCode.Success)
                    {
                        long BankID = this.InertEntity(new UsersBanksEntity()
                        {
                            Area = Area == null ? "" : Area,
                            BankName = BankName,
                            BankType = 0,
                            CardNumber = CardNum,
                            CreateTime = DateTime.Now,
                            UserID = UserCode,
                            ReservedPhone = Mobile == null ? "" : Mobile
                        });
                        new UserRedis().Insert_BankCardRedis(new udv_BankCard()
                        {
                            Area = Area,
                            UserCode = UserCode,
                            Mobile = Mobile,
                            BankCode = BankID,
                            BankName = BankName,
                            CardNum = CardNum
                        });
                        RecCode = (int)ResultCode.Success;
                    }
                    else if (VerifyResult == 1)
                        RecCode = (int)ResultCode.CodeError;
                    else
                        RecCode = (int)ResultCode.CodeInvalidation;
                }
                result = new JsonResult()
                {
                    Code = RecCode,
                    Msg = Common.GetDescription((ResultCode)RecCode)
                };
            }
            catch (Exception ex)
            {
                log.Write("绑定银行卡[BindBankCard]：" + ex.StackTrace, true);
                result = new JsonResult()
                {
                    Code = (int)ResultCode.SystemBusy,
                    Msg = Common.GetDescription(ResultCode.SystemBusy)
                };
            }

            return result;
        }
        /// <summary>
        /// 解绑
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="BankCode"></param>
        /// <returns></returns>
        public JsonResult UnBindBankCard(long UserCode, long BankCode)
        {
            JsonResult result = null;
            try
            {
                new UserRedis().Remove_BankCardRedis(UserCode, BankCode);
                this.DelEntity(BankCode);
                result = new JsonResult()
                {
                    Code = (int)ResultCode.Success,
                    Msg = Common.GetDescription(ResultCode.Success)
                };
            }
            catch (Exception ex)
            {
                log.Write("解绑银行卡错误[UnBindBankCard]：" + ex.StackTrace, true);
                result = new JsonResult()
                {
                    Code = (int)ResultCode.SystemBusy,
                    Msg = Common.GetDescription(ResultCode.SystemBusy)
                };
            }
            return result;
        }
        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="Token"></param>
        /// <param name="UserCode"></param>
        /// <param name="PageNumber"></param>
        /// <param name="RowsPerPage"></param>
        /// <returns></returns>
        public BankCardsResult BankCards(long UserCode, int PageNumber, int RowsPerPage)
        {
            BankCardsResult result = null;
            try
            {
                int RecCode = (int)ResultCode.Success;
                List<BankCardEntity> bankData = null;
                var BankEntitys = new UserRedis().Query_BankCardRedis(UserCode);
                if (BankEntitys == null || BankEntitys.Count == 0)
                {
                    var obj_Entity = dal.QueryEntitys(UserCode);
                    if (obj_Entity == null || obj_Entity.Count == 0)
                        RecCode = (int)ResultCode.NullData;
                    else
                        bankData = obj_Entity.Select(s => new BankCardEntity()
                        {
                            BankArea = s.Area,
                            BankCode = s.BankID,
                            BankName = s.BankName,
                            CardNum = s.CardNumber
                        }).ToList();
                }
                else
                    bankData = BankEntitys.Select(s => new BankCardEntity()
                    {
                        BankArea = s.Area,
                        BankCode = s.BankCode,
                        BankName = s.BankName,
                        CardNum = s.CardNum
                    }).ToList();
                result = new BankCardsResult()
                {
                    Code = RecCode,
                    Msg = Common.GetDescription((ResultCode)RecCode),
                    Data = RecCode != (int)ResultCode.Success ? null : bankData
                };
            }
            catch (Exception ex)
            {
                log.Write("查询银行卡列表错误[BankCards]：" + ex.StackTrace, true);
                result = new BankCardsResult()
                {
                    Code = (int)ResultCode.SystemBusy,
                    Msg = Common.GetDescription(ResultCode.SystemBusy),
                    Data = null
                };
            }
            return result;
        }
        #endregion
    }
}
